package com.joker.userExam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.userExam.dao.mapper.UserInfoMapper;
import com.joker.userExam.dao.repo.UserInfoRepo;
import com.joker.userExam.model.SubjectOneQuestion;
import com.joker.userExam.model.UserInfo;
import com.joker.userExam.service.IUserManage;
import com.joker.userExam.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 李杰
 * @date 2021/5/7 - 22:07
 */
@Service
public class UserManageImpl implements IUserManage {
    @Autowired
    UserInfoRepo userInfoRepo;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public ResultUtil queryUserInfo() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("type", 0);
        List<UserInfo> list = userInfoRepo.list(queryWrapper);
        return ResultUtil.ok("success", list);
    }

    @Override
    public ResultUtil queryUserInfoByCondition(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if (userInfo.getType() != null) {
            queryWrapper.eq("type", userInfo.getType());
        }
        if (!userInfo.getTrueName().isEmpty()) {
            queryWrapper.like("true_Name", userInfo.getTrueName());
        }
        return ResultUtil.ok("success", userInfoRepo.list(queryWrapper));
    }

    @Override
    public ResultUtil updateUserInfoById(UserInfo userInfo) {
        boolean res = userInfoRepo.updateById(userInfo);
        if (res) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil deleteUserInfoById(Integer id) {
        boolean res = userInfoRepo.removeById(id);
        if (res) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil queryAllCoach() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1);
        List<UserInfo> list = userInfoRepo.list(queryWrapper);
        return ResultUtil.ok("success", list);
    }

    @Override
    public ResultUtil saveUserInfo(UserInfo userInfo) {
        boolean res = userInfoRepo.save(userInfo);
        if (res) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }
}
